package com.yoo.ons.function

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible
import androidx.viewpager.widget.ViewPager
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.yoo.module_base.databinding.LayoutNavigationBinding
import com.yoo.module_base.utils.SaveDeviceUtil
import com.yoo.module_base.utils.setOnClickCallback
import com.yoo.module_res.R
import com.yoo.ons.util.Injections.animScaleKick

class NavigationLayout @JvmOverloads constructor(
        context: Context,
        attrs: AttributeSet? = null
) : ConstraintLayout(context, attrs) {
    val binding = LayoutNavigationBinding.inflate(LayoutInflater.from(context), this)


    private val itemViews by lazy { arrayOf(binding.tabHomeMakeFriends, binding.tabHomeActive,binding.tabHomeChat, binding.tabHomeProfile) }
    private val clickViews by lazy { arrayOf(binding.vTabFirst, binding.vTabSecond, binding.vTabThird, binding.vTabFour) }
    val menuCount: Int get() = itemViews.size
    private var currentIndex = -1
    private var pager: ViewPager? = null
    private var callback: Callback? = null

    interface Callback {
        fun onItemClick(position: Int): Boolean
        fun onItemClickAgain(position: Int)
    }

    init {
        clickViews.forEachIndexed { index, view ->
            view.setOnClickCallback { select(index) }
        }
    }


    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        pager?.currentItem?.let(::select)
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
    }

    fun select(target: Int) {
        Glide.with(this)
            .load(SaveDeviceUtil.getAvatarUrl())
            .apply(RequestOptions.bitmapTransform(CircleCrop()))
            .into(binding.tabHomeProfile)

        if (currentIndex == target) {
            val animId = when (currentIndex) {
                0 -> R.mipmap.img_one_select
                1 -> R.mipmap.img_two_select
                2 -> R.mipmap.img_three_select
                else -> R.mipmap.img_three_select
            }
            select(itemViews[target], animId,target)
            callback?.onItemClickAgain(target)
            return
        }
        if (callback?.onItemClick(target) != true) {
            currentIndex = target
            itemViews.forEachIndexed { index, view ->
                if (target == index) {
                    val animId = when (index) {
                        0 -> R.mipmap.img_one_select
                        1 -> R.mipmap.img_two_select
                        2 -> R.mipmap.img_three_select
                        else -> R.mipmap.img_three_select
                    }
                    select(view, animId,index)
                } else {
                    val restId = when (index) {
                        0 -> R.mipmap.img_one_unselect
                        1 -> R.mipmap.img_two_unselect
                        2 -> R.mipmap.img_three_unselect
                        else -> R.mipmap.img_three_unselect
                    }
                    reset(view, restId,index)
                }
            }
        }


    }


    fun attach(viewPager: ViewPager) {
        pager = viewPager
        viewPager.currentItem.let(::select)
    }

    fun setListener(listener: Callback?) {
        callback = listener
    }

    private fun select(imageView: ImageView, animationId: Int, index: Int) {
        if (index != 3){
            imageView.setImageResource(animationId)
        }else {
            imageView.setBackgroundResource(R.drawable.tab_me_select_bg)
        }
        imageView.startAnimation(animScaleKick)
    }

    private fun reset(imageView: ImageView, resId: Int, index: Int) {
        if (index != 3){
            imageView.setImageResource(resId)
        }else{
            imageView.background=null
        }
        imageView.clearAnimation()
    }

    /**
     * 设置未读消息
     */
    fun setUnreadNum(number:Int){
        binding.txtUnreadNum.isVisible=number>0
        binding.txtUnreadNum.text=if (number > 100) "99+" else "$number"
    }

    /**
     * 设置wlm次数
     */
    fun setWLMNum(number:Int){
        binding.txtWlmNum.isVisible=number>0
        binding.txtWlmNum.text=if (number > 100) "99+" else "$number"
    }

}